/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kks;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author basti
 */
public class cMovieList extends ArrayList<cMovie>
{
    
    public cMovieList()
    {
        
    }

    /**
     * Appends the specified element to the end of this list.
     *
     * @param e element to be appended to this list
     * @return <tt>1</tt>
     */    
    public int addMovies(cMovie movie)
    {
        try
        {
            add(movie);
            return 1;
        }
        catch(Exception e)
        {
            return 0;
        }                
    }
    
    public ArrayList<cMovie> getMoviesFromDB()
    {
        ArrayList<cMovie> movies = new ArrayList<>();
        DBuse dbuse = new DBuse();
        ArrayList<GregorianCalendar> times = new ArrayList<>(); 
        
        try 
        {           
            
            times.add(new GregorianCalendar(2013, 1, 1, 15, 00));
            times.add(new GregorianCalendar(2013, 1, 1, 19, 00));
            times.add(new GregorianCalendar(2013, 1, 1, 22, 00));

            ResultSet rset = dbuse.getMovies("select name,price,room_id,seats,seatrow from movie as m ,room as r where r.room_id = m.room_id");

            while (rset.next())
            {
                this.add(new cMovie(rset.getString("name"), rset.getInt("room_id"), rset.getInt("seatrow"), rset.getInt("seats"), rset.getDouble("price"), times));
            }
        } 
        catch (SQLException ex) 
        {
            Logger.getLogger(cMovieList.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        
        return movies;         
    }
    
    /**
     * adds all MoviePanels of the List to the given panel
     *<p>
     * 
     * @param panel the Panel to add to
     * 
     */
    public void addMoviePanels(HomeFrame homeFrame, JPanel panel)
    {        
        for (int i = 0; i < this.size()-1; i++)
        {
            panel.add(new PanelMovie(homeFrame, this.get(i)));                            
        }    
        
    }
    
}
